package com.it.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
// TODO SpringMVC (3)、接收请求头参数

@Controller
@RequestMapping("/cookie")
@ResponseBody
public class springmvc03 {
    // TODO 接收Cookie
    /**
     * 【说明】：接收cookie参数通过
     * 【语法】：@CookieValue(value = "cookieName")
     */
    @GetMapping("/data1")
    public String data1(@CookieValue(value = "cookieName") String val) {
        System.out.println("Cookie=" + val);
        return "Cookie=" + val;
    }
    // 先向浏览器获取存一个cookie，在通过data1方法获取
    @GetMapping("/data2")
    public String data2(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookieName", "cookierooot");
        response.addCookie(cookie);
        return "ok";
    }

    // TODO 获取请求头参数
    /**
     * 【说明】：获取请求头的参数
     * 【语法】：@RequestHeader("请求头参数的key")
     */
    @GetMapping("/data3")
    public String data3(@RequestHeader("Host") String host) {
        System.out.println(host);
        return "Host=" + host;
    }
}

